$(function() {
    var layer = layui.layer
    var form = layui.form
    $.ajaxPrefilter(function(option) {
        option.url = 'http://ajax.frontend.itheima.net' + option.url
        if (option.url.indexOf('/my') !== -1) {
            option.headers = {
                Authorization: localStorage.getItem('token') || ''
            }
            option.complete = function(res) {
                if (res.responseJSON.status == 1 && res.responseJSON.message === '身份认证失败！') {
                    localStorage.setItem('token', '')
                    location.href = './login.html'
                }
            }
        }
    })
    initArtCateList()

    function initArtCateList() {
        $.ajax({
            method: 'GET',
            url: '/my/article/cates',
            success: function(res) {

                var htmlStr = template('tpl-table', res)
                $('tbody').html(htmlStr)
            }
        })
    }
    var indexAdd = null
    $('#btnAddCate').on('click', function() {
        indexAdd = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '添加文章类别',
            content: $('#dialog-add').html()
        });
    })
    $('body').on('submit', '#form-add', function(e) {
        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/addcates',
            data: $(this).serialize(),
            success: function(res) {
                if (res.status !== 0) {
                    return layer.msg('新增分类失败！')
                }
                layer.msg('新增分类成功！')
                layer.close(indexAdd)
                initArtCateList()
            }
        })
    })
    var indexEdit = null
    $('tbody').on('click', '#btn-edit', function() {
        indexEdit = layer.open({
            type: 1,
            area: ['500px', '250px'],
            title: '修改文章类别',
            content: $('#dialog-edit').html()
        });
        var id = $(this).attr('data-id')
        console.log($(this).data('id'));
        $.ajax({
            method: 'GET',
            url: '/my/article/cates/' + id,
            success: function(res) {
                console.log(res);
                form.val('form-edit', res.data)
            }
        })
    })
    $('body').on('submit', '#form-edit', function(e) {

        e.preventDefault()
        $.ajax({
            method: 'POST',
            url: '/my/article/updatecate',
            data: $(this).serialize(),
            success: function(res) {

                if (res.status !== 0) {
                    return layer.msg('更新分类数据失败！')
                }
                layer.msg('更新分类数据成功！')
                layer.close(indexEdit)
                initArtCateList()
            }
        })

    })
    $('tbody').on('click', '.btn-delete', function() {
        var id = $(this).attr('data-id')
        layer.confirm('确定删除?', { icon: 3, title: '提示' }, function(index) {
            // setInterval(function() {
            //     $.ajax({
            //         type: 'GET',
            //         url: '/my/article/cates',
            //         success: function(res) {

            //             num = res.data.length - 1
            //             console.log(num);
            //             for (var i = num; i > 1; i--) {
            //                 var index = res.data[i].Id
            //                 console.log(index);
            //                 $.get('/my/article/deletecate/', {
            //                     Id: index
            //                 }, function(res) {

            //                 })
            //             }





            $.ajax({
                    method: 'GET',
                    url: '/my/article/deletecate/' + id,

                    success: function(res) {
                        if (res.status !== 0) {
                            return layer.msg('删除分类失败！')
                        }
                        layer.msg('删除分类成功！')

                        layer.close(index)
                        initArtCateList()
                    }
                })
                // }, 1)
        })
    })
})